﻿using HDS.Admin.Api.ViewModels.UpdateLog;
using HDS.IService.Log;
using HDS.Model;
using HDS.Model.Entity.Log;
using LinqKit;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace HDS.Admin.Api.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    [EnableCors]
    public class UpdateLogController : AdminController
    {
        private readonly ILogger<UpdateLogController> _logger;
        private readonly IUpdateLogService _IUpdateLogService;

        public UpdateLogController(ILogger<UpdateLogController> logger, IUpdateLogService iupdateLogService)
        {
            _logger = logger;
            _IUpdateLogService = iupdateLogService;
        }

        public async Task<ResultData<string>> Add(UpdateLog entity)
        {
            entity.UpdatedBy = this.UserId;
            entity.UpdatedByName = this.UserName;
            entity.UpdatedTime = DateTime.Now;
            await _IUpdateLogService.Add(entity);

            return new ResultData<string> { Success = true, Data = "" };
        }

        public async Task<ResultData<DataPagingModel<UpdateLog>>> GetList([FromBody] SearchDto searchDto)
        {
            var predicate = PredicateBuilder.New<UpdateLog>(true);
            if (!string.IsNullOrEmpty(searchDto.BusinessId))
                predicate = predicate.And(e => e.BusinessId == searchDto.BusinessId);
            if (!string.IsNullOrEmpty(searchDto.LogSubType))
                predicate = predicate.And(e => e.LogSubType == searchDto.LogSubType);
            if (searchDto.LogType.HasValue)
                predicate = predicate.And(e => e.LogType == searchDto.LogType);

            var dataPaging = await _IUpdateLogService.GetList(searchDto.PageIndex, searchDto.PageSize, predicate);

            return new ResultData<DataPagingModel<UpdateLog>> { Success = true, Data = dataPaging };
        }
    }
}
